

import java.util.*;

public class BinaryTreeCreate {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        //TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;
        return A;
    }

    //二叉树的遍历
    //1、前序遍历    根 -> 左子树 -> 右子树
    //2、中序遍历    左子树 -> 根 -> 右子树
    //3、后序遍历    左子树 -> 右子树 -> 根
    //4、层次遍历    自上而下 从左到右

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //非递归 前序遍历
    public  void preOrderNor(TreeNode root) {
        if (root == null) return;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            //cur == null  此时cur的左边走完 要判断右边
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }





    /*
    遍历思路 - 但没有运用list这个返回值
    List<Integer> ret = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {

        if (root == null) {
            return null;
        }
        ret.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);

        return ret;
    }*/


    /*子问题  此时运用到了 list的返回值 全部都放到了list里面
    List<Integer> ret = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {

        if (root == null) {
            return null;
        }
        ret.add(root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        ret.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.left);
        return ret;
    }*/

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");

    }

    // 获取树中节点的个数
    public static int usedSize = 0;
    public int size(TreeNode root){
        if (root == null) return 0;
        ++usedSize;
        size(root.left);
        size(root.right);
        return usedSize;
    }

    public int size2(TreeNode root) {
        if (root == null) return 0;
        /*int ret = size2(root.left) + size2(root.right) + 1;
        return ret;*/
        return size2(root.left) + size2(root.right) + 1;
    }

    // 获取叶子节点的个数
    public static int len = 0;
    public int getLeafNodeCount(TreeNode root){
        if (root == null) return 0;
        if (root.left == null && root.right == null) {
            ++len;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return len;
    }


    public int getLeafNodeCount2(TreeNode root){
        if (root == null) return 0;
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k-1)
                + getKLevelNodeCount(root.right, k-1);
    }

    // 获取二叉树的高度
    public int getHeight(TreeNode root){
        if (root == null) return 0;
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);

        return (leftH > rightH ? leftH : rightH) + 1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val) {
        if (root == null) return null;
        if (root.val == val) {
            return root;
        }

        TreeNode leftL = find(root.left, val);
        if (leftL != null) {
            return leftL;
        }
        TreeNode rightR = find(root.right, val);
        if (rightR != null) {
            return rightR;
        }
        return null;
    }

    //层序遍历
    public  void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val + " ");

            if (top.left != null) {
                queue.offer(top.left);
            }
            if (top.right != null) {
                queue.offer(top.right);
            }
        }
    }

    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>>  ret = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            int size = queue.size();//这一层节点的个数
            List<Integer> list = new ArrayList<>();
            while (size != 0) {
                TreeNode top = queue.poll();
                //System.out.print(top.val + " ");
                //list.add(top.val);
                if (top.left != null) {
                    queue.offer(top.left);
                }
                if (top.right != null) {
                    queue.offer(top.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root != null) {
            queue.offer(root);
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                return false;
            }
        }
        return true;
    }
}
